package cn.xshi.sys.service.impl;

import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.common.util.JsonUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.common.util.file.FileUtil;
import cn.xshi.sys.dao.SysPathDao;
import cn.xshi.sys.form.SysPathForm;
import cn.xshi.sys.init.GlobalPersistentComponent;
import cn.xshi.sys.model.SysPath;
import cn.xshi.sys.service.SysPathService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
/**
 * @Desc 文件路径
 * @Author 邓纯杰
 * @CreateTime 2024-04-24 09:38:12
 */
@Service
@Slf4j
public class SysPathServiceImpl extends BaseService implements SysPathService {

	@Resource
	private SysPathDao sysPathDao;

	@Resource
    GlobalPersistentComponent globalPersistentComponent;

	/**
	 * 分页
	 * @param sysPathForm
	 * @return
	 */
	public List<SysPath> getSysPathListByCondition(SysPathForm sysPathForm){
		try {
			return sysPathDao.getSysPathListByCondition(sysPathForm);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public SysPath getSysPathById(String id){
		try {
			return sysPathDao.getSysPathById(id);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param sysPath
	* @return
	*/
	public int addSysPath(SysPath sysPath){
		int i = 0;
		try {
			sysPath.setCreateId(getCurrentAccountId());
			sysPath.setCreateTime(getDate());
			i = sysPathDao.addSysPath(sysPath);
			globalPersistentComponent.initPath();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param sysPath
	* @return
	*/
	public int updateSysPath(SysPath sysPath){
		int i = 0;
		try {
			sysPath.setUpdateId(getCurrentAccountId());
			sysPath.setUpdateTime(getDate());
			i = sysPathDao.updateSysPath(sysPath);
			globalPersistentComponent.initPath();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delSysPath(Map<String,Object> condition){
		int i = 0;
		try {
			i = sysPathDao.delSysPath(condition);
			globalPersistentComponent.initPath();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 查找所有平台路径
	 * @param condition
	 * @return
	 */
	public List<SysPath> getSysPathListAllByCondition(Map<String,Object> condition){
		try {
			return sysPathDao.getSysPathListAllByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 导出
	 * @param response
	 * @param id
	 */
	public void exportPath(HttpServletResponse response, String id){
		try{
			if(StringUtil.isEmpty(id)){
				log.warn("未能获取id");
				throw new ExceptionUtil("未能获取id");
			}
			SysPath sysPath = getSysPathById(id);
			if(null == sysPath){
				log.warn("未能获取到平台路径信息");
				throw new ExceptionUtil("未能获取到平台路径信息");
			}
			String json = JsonUtil.toFastJson(sysPath);
			FileUtil.exportJsonFile(response,json,sysPath.getName()+"-"+sysPath.getName()+".json");
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 导入
	 * @param request
	 * @param response
	 * @param multipartFile
	 * @return
	 */
	public BaseResult importPath(HttpServletRequest request, HttpServletResponse response, MultipartFile multipartFile){
		String jsonText = FileUtil.readJson(multipartFile);
		if(StringUtil.isEmpty(jsonText)){
			return BaseResult.fail("导入内容为空！");
		}
		SysPath sysPath = JsonUtil.fromAliFastJson(jsonText,SysPath.class);
		if(null == sysPath){
			return BaseResult.fail("未能解析到平台路径信息！");
		}
		if(StringUtil.isEmpty(sysPath.getId())){
			return BaseResult.fail("未能解析到平台路径id！");
		}
		if(StringUtil.isEmpty(sysPath.getName())){
			return BaseResult.fail("未能解析到平台路径名称！");
		}
		SysPath path = getSysPathById(sysPath.getId());
		if(null == path){//新增
			addSysPath(sysPath);
		}else{
			sysPath.setUpdateId(getCurrentAccountId());
			sysPath.setUpdateTime(getDate());
			updateSysPath(sysPath);
		}
		return BaseResult.success();
	}
}
